<template>
  <div id="userUpdatePage">
    <h2 class="title">汉堡王云图库 - 修改个人信息</h2>
    <div class="des">企业级智能协同云图库</div>
    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="handleSubmit"
    >
      <a-form-item
        label="用户头像"
        name="avatar"
      >
        <file-upload :user="loginUser" :biz="fileBiz"></file-upload>
      </a-form-item>
      <a-form-item
        label="用户昵称"
        name="userName"
        :rules="[{ message: '请输入昵称!' }]"
      >
        <a-input v-model:value="formState.userName" placeholder="请输入昵称" />
      </a-form-item>

      <a-form-item
        label="邮箱"
        name="email"
        :rules="[{ message: '请输入邮箱!' },{type: 'email', message: '请输入正确的邮箱格式!'}]"
      >
        <a-input v-model:value="formState.email" placeholder="请输入邮箱" />
      </a-form-item>

      <a-form-item
        label="简介"
        name="userProfile"
      >
        <a-textarea v-model:value="formState.userProfile" placeholder="请输入简介" :rows="4"/>
      </a-form-item>

      <a-button type="primary" html-type="submit" style="width: 100%">提交修改</a-button>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { updateMyUserUsingPost, userLoginUsingPost } from '@/api/userController.ts'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { message, type UploadProps } from 'ant-design-vue'
import router from '@/router'
import PictureUpload from '@/components/PictureUpload.vue'
import { LoadingOutlined, PlusOutlined } from '@ant-design/icons-vue'
import { uploadFileUsingPost } from '@/api/fileController.ts'
import FileUpload from '@/components/FileUpload.vue'

const loginUserStore = useLoginUserStore()
const loginUser = loginUserStore.loginUser
//用于接受表单接受的值
const formState = reactive<API.UserUpdateRequest>({
  id: loginUserStore.loginUser.id,
  userName: loginUserStore.loginUser.userName,
  userProfile: loginUserStore.loginUser.userProfile,
  userAvatar: loginUserStore.loginUser.userAvatar,
  email: loginUserStore.loginUser.email,
})


const handleSubmit = async () => {
  try {
    const res = await updateMyUserUsingPost(formState)
    if (res.data.code===0 && res.data.data){
      message.success("更新成功")
      router.push({
        path:"/user/update",
        replace:true
      })
    }else {
      message.error("登录失败"+ res.data.message)
    }
  }catch (e){
    message.error("登录失败"+ e.message)
  }
}
const fileBiz = 'user_avatar'

/**
 * 上传图片
 * @param file
 */
const handleUpload = async ({file}:any)=>{
  loading.value = true;
  try {
    const res =  await uploadFileUsingPost(fileBiz.value,file);
    if (res.data.code === 0 && res.data.data){
      message.success("图片上传成功");
    }else {
      message.error("图片上传失败" + res.data.message)
    }
  }catch (error){
    console.error("图片上传失败，"+error)
    message.error("图片上传失败" + error.message)
  }
  loading.value = false;
}

const loading = ref<boolean>(false);

const handleAvatarClick = ()=>{
  console.log("点击了头像")
}


/**
 * 上传前的校验
 * @param file
 */
const beforeUpload = (file: UploadProps['fileList'][number]) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('不支持上传该格式的图片，推荐 jpg 或 png!');
  }
  const isLt2M = file.size / 1024 / 1024 < 4;
  if (!isLt2M) {
    message.error('不能上传超过2MB的图片!');
  }
  return isJpgOrPng && isLt2M;
};
</script>

<style scoped>
#userUpdatePage{
  max-width: 360px;
  margin: 0 auto;
}

.title{
  text-align: center;
  margin-bottom: 16px;
}

.des{
  color: #bbb;
  margin-bottom: 16px;
  text-align: center;
}

.tips{
  text-align: right;
  color: #bbb;
  font-size: 13px;
}
</style>

